﻿using GalaSoft.MvvmLight;
using MyImdb.Common;
using MyImdb.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MyImdb.ViewModel
{
    internal class SearchLocalViewModel : ViewModelBase
    {
        /// <summary>
        /// The _ imdb titles
        /// </summary>
        private ObservableCollection<ImdbItem> _ImdbTitles;
        /// <summary>
        /// Gets or sets the imdb titles.
        /// </summary>
        /// <value>
        /// The imdb titles.
        /// </value>
        public ObservableCollection<ImdbItem> ImdbTitles
        {
            get { return _ImdbTitles; }
            set
            {
                _ImdbTitles = value;
                RaisePropertyChanged("ImdbTitles");
            }
        }

        private Visibility _DetailsVisibility;
        public Visibility DetailsVisibility
        {
            get { return _DetailsVisibility; }
            set
            {
                _DetailsVisibility = value;
                RaisePropertyChanged("DetailsVisibility");
            }
        }

        private string _Rating;
        public string Rating
        {
            get { return _Rating; }
            set
            {
                _Rating = value;
                RaisePropertyChanged("Rating");
            }
        }

        private string _Type;
        public string Type
        {
            get { return _Type; }
            set
            {
                _Type = value;
                RaisePropertyChanged("Type");
            }
        }

        private Visibility _EpisodesVisibility;
        public Visibility EpisodesVisibility
        {
            get { return _EpisodesVisibility; }
            set
            {
                _EpisodesVisibility = value;
                RaisePropertyChanged("EpisodesVisibility");
            }
        }

        private ObservableCollection<int> _Seasons;
        public ObservableCollection<int> Seasons
        {
            get { return _Seasons; }
            set
            {
                _Seasons = value;
                RaisePropertyChanged("Seasons");
            }
        }

        private int _SelectedSeason;
        public int SelectedSeason
        {
            get { return _SelectedSeason; }
            set
            {
                _SelectedSeason = value;
                if (_SelectedSeason != -1)
                {
                    Episodes = SelectedImdbTitle.Episodes.Where(t => t.Season == _SelectedSeason).
                        OrderBy(t => t.EpisodeSL).
                        OrderBy(t => t.Season).ToObservableCollection();
                }
                RaisePropertyChanged("SelectedSeason");
            }
        }

        private ObservableCollection<Episode> _Episodes;
        public ObservableCollection<Episode> Episodes
        {
            get { return _Episodes; }
            set
            {
                _Episodes = value;
                RaisePropertyChanged("Episodes");
            }
        }

        private string _RunTime;
        public string RunTime
        {
            get { return _RunTime; }
            set
            {
                _RunTime = value;
                RaisePropertyChanged("RunTime");
            }
        }

        /// <summary>
        /// The _ selected imdb title
        /// </summary>
        private ImdbItem _SelectedImdbTitle;
        /// <summary>
        /// Gets or sets the selected imdb title.
        /// </summary>
        /// <value>
        /// The selected imdb title.
        /// </value>
        public ImdbItem SelectedImdbTitle
        {
            get { return _SelectedImdbTitle; }
            set
            {
                _SelectedImdbTitle = value;
                if (_SelectedImdbTitle != null)
                {
                    DetailsVisibility = Visibility.Visible;
                    Rating = string.Format("{0}/10 by {1} users", _SelectedImdbTitle.Rating, _SelectedImdbTitle.RatingCount);
                    Type = _SelectedImdbTitle.Episodes == null ? "Movie" : "TV Series";
                    EpisodesVisibility = _SelectedImdbTitle.Episodes == null ? Visibility.Collapsed : Visibility.Visible;
                    if (_SelectedImdbTitle.Episodes != null)
                    {
                        var episodes = _SelectedImdbTitle.Episodes.
                            OrderBy(t => t.EpisodeSL).
                            OrderBy(d => d.Season).ToList();

                        var lastSeason = episodes[episodes.Count - 1].Season;

                        Seasons = new ObservableCollection<int>();
                        for (int seasonNo = 1; seasonNo <= lastSeason; seasonNo++)
                        {
                            Seasons.Add(seasonNo);
                        }
                        SelectedSeason = 1;
                    }

                    RunTime = _SelectedImdbTitle.RunTime[0];
                }
                else
                {
                    DetailsVisibility = Visibility.Collapsed;
                    Rating = string.Empty;
                    Type = string.Empty;
                }
                RaisePropertyChanged("SelectedImdbTitle");
            }
        }

        private Settings _AppSettings;
        public Settings AppSettings
        {
            get { return _AppSettings; }
            set
            {
                _AppSettings = value;
                RaisePropertyChanged("AppSettings");
            }
        }

        public SearchLocalViewModel()
        {
            ImdbTitles = new ObservableCollection<ImdbItem>();
            ReadSettings();

            if (FileHelper.IsDatabaseExist(AppSettings.DatabasePath))
            {
                FileHelper.DatabaseBasePath = AppSettings.DatabasePath;

                var titleList = FileHelper.ReadTitles();
                if (titleList != null)
                {
                    foreach (var title in titleList)
                    {
                        var imdbTitle = FileHelper.ReadTitle(title);
                        if (imdbTitle != null)
                        {
                            ImdbTitles.Add(imdbTitle);
                        }
                    }
                    ImdbTitles = ImdbTitles.OrderBy(t => t.Title).ToObservableCollection();
                }
            }
            else
            {
                //No Database found
            }

            DetailsVisibility = Visibility.Collapsed;
        }

        private void ReadSettings()
        {
            AppSettings = FileHelper.ReadSettings();
        }
    }
}
